package com.oujia.cloud.cloudcommon.bean.config;


import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class RabbitMqConfig {

    @Value("${spring.rabbitmq.host}")
    private String host;
    @Value("${spring.rabbitmq.port}")
    private int port;
    @Value("${spring.rabbitmq.username}")
    private String username;
    @Value("${spring.rabbitmq.password}")
    private String password;
    @Value("${spring.rabbitmq.publisher-confirms}")
    private boolean publisherConfirms;
    @Value("${spring.rabbitmq.virtual-host}")
    private String virtualHost;
    @Value("${spring.rabbitmq.max-concurrent-consumers}")
    private int maxConcurrentConsumers;
    @Value("${spring.rabbitmq.concurrent-consumers}")
    private int concurrentConsumers;


    //日志服务
    @Value("${spring.rabbitmq.queue.log}")
    private String queueLog;
    @Value("${spring.rabbitmq.routingkey.log}")
    private String routingKeyLog;

    @Value("${spring.rabbitmq.exchange.topic}")
    private String topicExchangeName;

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host,port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);
        connectionFactory.setPublisherConfirms(publisherConfirms);

        return connectionFactory;
    }

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    //必须是prototype类型
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
        MessageConverter converter = new Jackson2JsonMessageConverter();
        rabbitTemplate.setMessageConverter(converter);
        return rabbitTemplate;
    }

    /**
     * 针对消费者配置
     * 1. 设置交换机类型
     * 2. 将队列绑定到交换机
     *
     *
     FanoutExchange: 将消息分发到所有的绑定队列，无routingKey的概念
     HeadersExchange ：通过添加属性key-value匹配
     DirectExchange:按照routingKey分发到指定队列
     TopicExchange:多关键字匹配
     */
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(topicExchangeName);
    }
    //可以持久化多个个key
    @Bean
    public Queue queueLog() {
        return new Queue(queueLog, true); //队列持久
    }


    //绑定
    @Bean
    public Binding logBind() {
        return BindingBuilder.bind(queueLog()).to(topicExchange()).with(routingKeyLog);
    }



    public String getTopicExchangeName() {
        return topicExchangeName;
    }

    public String getQueueLoge() {
        return queueLog;
    }

    public String getRoutingKeyLog() {
        return routingKeyLog;
    }

    public int getMaxConcurrentConsumers() {
        return maxConcurrentConsumers;
    }

    public int getConcurrentConsumers() {
        return concurrentConsumers;
    }

}
